package com.lmy.springboot.controller;

import com.lmy.springboot.bean.Monster;
import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSUtil;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

@RestController
@Slf4j
public class ParameterController {

    /**
     * 使用@PathVariable可以直接接收一个map，把所有传递的值传入map
     * @param userId 单个接收
     * @param userName 单个接收
     * @param map 接收所有请求值
     * @return
     */
    @RequestMapping(value = "/monster/{id}/{name}", method = RequestMethod.GET)
    public String pathVariable(@PathVariable(value = "id") Integer userId,
                               @PathVariable(value = "name") String userName,
                               @PathVariable Map<String, String> map) {
        System.out.println("id-" + userId);
        System.out.println("name-" + userName);
        System.out.println("map-" + map);
        return "success";
    }

    /**
     * 使用@RequestHeader获取http请求头相关信息
     * @param host  获取host信息
     * @param map 获取所有http请求头信息，封装到map中
     * @return
     */
    @RequestMapping(value = "/requestHeader")
    public String requestHeader(@RequestHeader("Host") String host,
                                @RequestHeader Map<String, String> map) {
        System.out.println("host-" + host);
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String value = map.get(key);
            System.out.println("key-" + key + "|value-" + value);
        }
        return "success";
    }

    /**
     * 使用@RequestParam获取对应名称的请求参数
     * @param name 获取请求参数 name的值
     * @param fruitList 获取多个参数名为fruit的值，封装到list中
     * @param map 获取所有请求值，当有多个相同的参数名对应的参数值时，只会获取一个。需要注意
     * @return
     */
    @RequestMapping(value = "/requestParameter")
    public String requestParameter(@RequestParam(value = "name") String name,
                                   @RequestParam(value = "fruit") List<String> fruitList,
                                   @RequestParam Map<String, Objects> map){
        System.out.println("name-" + name);
        System.out.println("fruitList-" + fruitList);
        System.out.println("map-" + map);
        return "success";
    }

    /**
     * 使用@CookieValue获取浏览器存放的cookie值(可以人为在浏览器设置cookie，方便测试)
     * @param cookieValue 获取浏览器cookie中 key为cookie_key的value值
     * @param cookie 获取浏览器cookie中 key为username对应的Cookie对象
     * @return
     */
    @RequestMapping("/cookie")
    public String cookie(@CookieValue(value = "cookie_key", required = false) String cookieValue,
                        @CookieValue(value = "username", required = false) Cookie cookie,
                         HttpServletRequest request) {
        System.out.println("cookieValue-" + cookieValue);
        System.out.println("usernameCookie-" + cookie);
        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies) {
            System.out.println(c.getName() + "=>" + c.getValue());
        }
        return "success";
    }

    /**
     * 使用@RequestBody 可以获取请求体内容
     * @param content
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@RequestBody String content) {
        System.out.println(content);
        return "success";
    }

    /**
     * 在开发中，SpringBoot在响应客户端/浏览器请求时，也支持自定义对象参数
     * 会自动完成类型转换与数据格式化，还可以完成级联封装
     * @param monster 自定义封装的对象
     * @return
     */
    @RequestMapping(value = "/saveMonster", method = RequestMethod.POST)
    public String saveMonster(Monster monster) {
        log.info("monster: " + monster);
        return "success";
    }

}
